﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;

namespace Waiter.Controler
{
    class CommonCtrl
    {
        /// <summary>
        /// set selected item of combobox by index
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="index"></param>
        public static void fsCmbSetSelectedIndex(ComboBox cmb, int index)
        {
            if (cmb.Items.Count <= 0)
            {
                return;
            }

            if (cmb.Items.Count > index)
            {
                cmb.SelectedIndex = index;
            }
            else
            {
                cmb.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// set selected item of combobox by value
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="value"></param>
        public static void fsCmbSetSelectedValue(ComboBox cmb, string value)
        {
            try
            {
                cmb.SelectedValue = value;
            }
            catch (Exception ex)
            {
                // ghi log
            }
        }

        /// <summary>
        /// set selected item of combobox by text
        /// </summary>
        /// <param name="cmb"></param>
        /// <param name="text"></param>
        public static void fsCmbSetSelectedText(ComboBox cmb, string text)
        {
            try
            {
                cmb.Text = text;
            }
            catch (Exception ex)
            {
                // ghi log
            }
        }

        /// <summary>
        /// get selected value of combobox
        /// </summary>
        /// <param name="cmb"></param>
        /// <returns></returns>
        public static string fsCmbGetSelectedValue(ComboBox cmb)
        {
            try
            {
                ComboboxItem item = (ComboboxItem)cmb.SelectedItem;
                if (item != null)
                {
                    return item.Value;
                }
            }
            catch(Exception ex)
            {
                // ghi log
            }
            return null;
        }

        /// <summary>
        /// get selected text of combobox
        /// </summary>
        /// <param name="cmb"></param>
        /// <returns></returns>
        public static string fsCmbGetSelectedText(ComboBox cmb)
        {
            try
            {
                ComboboxItem item = (ComboboxItem)cmb.SelectedItem;
                if (item != null)
                {
                    return item.Text;
                }
            }
            catch (Exception ex)
            {
                // ghi log
            }
            return null;
        }

        /// <summary>
        /// set selected row of datagridview
        /// </summary>
        /// <param name="rowIndex"></param>
        public static void fsDgrSetSelectedRow(DataGridView dgr, int rowIndex, int columIndex)
        {
            try
            {
                dgr.CurrentCell = dgr[columIndex, rowIndex];
                dgr.Rows[rowIndex].Selected = true;
            }
            catch (Exception ex)
            {
                // ghi log
            }
        }
    }
}
